package com.zhwei.gulimall.order.controller;

import com.zhwei.gulimall.order.entity.OrderEntity;
import com.zhwei.gulimall.order.entity.OrderReturnReasonEntity;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

/**
 * 我们定义个controlller来发布消息，不用juit
 */
@RestController
public class RabbitMqController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/sendMq")
    public String sendMq(@RequestParam(value = "num", defaultValue = "10") Integer num){
        for (int i = 0; i < num; i++) {
            //模拟这个队列可以发多种消息类型
            if(i % 2 == 0){
                OrderReturnReasonEntity rease = new OrderReturnReasonEntity();
                rease.setId(Long.valueOf(i));
                rease.setCreateTime(new Date());
                rease.setName("消息标题"+i);
                //注意最后的这个参数是id，如果发送失败的时候，可以在消息确认中获取到。然后再可以进行下一步的业务处理
                rabbitTemplate.convertAndSend("hello-java.exchage","hello.java",
                        rease, new CorrelationData(UUID.randomUUID().toString()));
            }else{
                OrderEntity orderEntity = new OrderEntity();
                orderEntity.setOrderSn(UUID.randomUUID().toString());
                rabbitTemplate.convertAndSend("hello-java.exchage","hello.java22",
                        orderEntity,new CorrelationData(UUID.randomUUID().toString()));
            }
        }
        return "ok";
    }
}
